<?php
	namespace Common\Logic;
	require_once 'vendor/qiniu/autoload.php';
	// 引入鉴权类
	use Qiniu\Auth;
	// 引入上传类
   use Qiniu\Storage\UploadManager;
	class QiniuLogic{
		static $accessKey = 'ta-jsNokMxaGtqXiXjy5O_yJzCq8t6O6vymm128z';
		static $secretKey = 'fYni4A1h41erk9Rm3pD97zBigA3RKGdQf4VcjAHu';
		static $bucket = 'yanshiji';
		static $qiniuUrl = 'https://images.nbmist.com/';
		/**
		 * 取得AcsClient
		 * @return DefaultAcsClient
		 */
		public function uploadFile($filePath = '',$ext='jpg'){
		    if(empty($filePath)){
		        return ['code'=>0,'errorMsg'=>'文件不存在'];
		    }
		    // 构建鉴权对象
            $auth = new Auth(static::$accessKey, static::$secretKey);
            // 生成上传 Token
            $token = $auth->uploadToken(static::$bucket);
            // 要上传文件的本地路径
            // $filePath = './Uploads/1.jpg';
            // 上传到存储后保存的文件名
            $extension = $ext;
            $key = 'zhenheyujia/'.date('YmdHis',time()).rand(1000,9999).'.'.$extension;
            // 初始化 UploadManager 对象并进行文件的上传。
            $uploadMgr = new UploadManager();
            // 调用 UploadManager 的 putFile 方法进行文件的上传。
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            if($err !== null){
                return ['code'=>0,'errorMsg'=>'上传失败：'.$err];
            }else{
                return ['code'=>1,'errorMsg'=>'上传成功','fileUrl'=>static::$qiniuUrl.$ret['key']];
            }
		}
	}
?>